﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AutoRotate : MonoBehaviour
{
        [Header("旋转速度")]
        public float speed = 34f;

        [Header("顺时针旋转")]
        public bool isClockWise = false;

        [Header("绕X轴旋转")]
        public bool isRotateX = false;
        [Header("绕Y轴旋转")]
        public bool isRotateY = true;
        [Header("绕Z轴旋转")]
        public bool isRotateZ = false;
        [Header("旋转等待时间")]
        public int waitTime = 3;

        public bool isUseAuto;

        //真实的速度
        float curSpeed = 0;



        void Start()
        {
                isUseAuto = true;
        }

        void FixedUpdate()
        {
                isUseAuto = SettingMain.isUseAutoRotate;

                if (!isUseAuto)
                {
                        curSpeed = 0;
                }

                if (isUseAuto)
                {
                        do
                        {
                                if (curSpeed > speed)
                                {
                                        curSpeed -= 4;
                                }
                                if (curSpeed < speed)
                                {
                                        curSpeed += 4;
                                }
                        } while (false);

                        float trueSpeed = 1 * Time.deltaTime * curSpeed;

                        //顺逆时针处理
                        if (!isClockWise)
                        {
                                trueSpeed *= -1;
                        }

                        //X方向旋转
                        if (isRotateX)
                        {
                                this.transform.Rotate(new Vector3(trueSpeed, 0, 0));
                        }
                        //Y方向旋转
                        if (isRotateY)
                        {
                                this.transform.Rotate(new Vector3(0, trueSpeed, 0));
                        }
                        //Z方向旋转
                        if (isRotateZ)
                        {
                                this.transform.Rotate(new Vector3(0, 0, trueSpeed));
                        }
                }
        }

        void Update()
        {
                if (!gameObject.activeSelf)
                {
                        isUseAuto = true;
                }
                DelayUseAuto();
        }

        //关闭自动的请求
        public static bool closeAutoFlag = false;
        
        private long startTime;
        /// <summary>
        /// 延时用自动
        /// </summary>
        void DelayUseAuto()
        {
                if (!isUseAuto)
                {
                        Debug.Log("检测时间");
                        long nowTime = MinuyTools.ConvertDateTimeToLong(System.DateTime.Now);
                        if ((nowTime - startTime) > waitTime)
                        {
                                Debug.Log("超时");
                                isUseAuto = true;
                        }
                }

                if (closeAutoFlag)
                {
                        Debug.Log("收到停止自传请求，更新时间戳！");
                        startTime = MinuyTools.ConvertDateTimeToLong(System.DateTime.Now);
                        closeAutoFlag = false;
                        isUseAuto = false;
                }
        }
}
